home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / fold.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  21.3 KB  |  567 lines

  1. *fold.txt*      For Vim version 6.0.  Last change: 2001 Sep 18
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Folding                        *Folding* *folding*
  8.  
  9. You can find an introduction on folding in chapter 28 of the user manual.
  10. |usr_28.txt|
  11.  
  12. 1. Fold methods        |fold-methods|
  13. 2. Fold commands    |fold-commands|
  14. 3. Fold options        |fold-options|
  15. 4. Behavior of folds    |fold-behavior|
  16.  
  17. {Vi has no Folding}
  18. {not available when compiled without the +folding feature}
  19.  
  20. ==============================================================================
  21. 1. Fold methods                    *fold-methods*
  22.  
  23. The folding method can be set with the 'foldmethod' option.
  24.  
  25. When setting 'foldmethod' to a value other than "manual", all folds are
  26. deleted and new ones created.  Switching to the "manual" method doesn't remove
  27. the existing folds.  This can be used to first define the folds automatically
  28. and then change them manually.
  29.  
  30. There are six methods to select folds:
  31.     manual        manually define folds
  32.     indent        more indent means a higher fold level
  33.     expr        specify an expression to define folds
  34.     syntax        folds defined by syntax highlighting
  35.     diff        folds for unchanged text
  36.     marker        folds defined by markers in the text
  37.  
  38.  
  39. MANUAL                        *fold-manual*
  40.  
  41. Use commands to manually define the fold regions.  This can also be used by a
  42. script that parses text to find folds.
  43.  
  44. The level of a fold is only defined by its nesting.  To increase the fold
  45. level of a fold for a range of lines, define a fold inside it that has the
  46. same lines.
  47.  
  48. The manual folds are lost when you abandon the file.  To save the folds use
  49. the |:mkview| command.  The view can be restored later with |:loadview|.
  50.  
  51.  
  52. INDENT                        *fold-indent*
  53.  
  54. The folds are automatically defined by the indent of the lines.
  55.  
  56. The foldlevel is computed from the indent of the line, divided by the
  57. 'shiftwidth' (rounded down).  A sequence of lines with the same or higher fold
  58. level form a fold, with the lines with a higher level forming a nested fold.
  59.  
  60. The nesting of folds is limited with 'foldnestmax'.
  61.  
  62. Some lines are ignored and get the fold level of the line above or below it,
  63. whatever is the lowest.  These are empty or white lines and lines starting
  64. with a character in 'foldignore'.  White space is skipped before checking for
  65. characters in 'foldignore'.  For C use "#" to ignore preprocessor lines.
  66.  
  67. When you want to ignore lines in another way, use the 'expr' method.  The
  68. |indent()| function can be used in 'foldexpr' to get the indent of a line.
  69.  
  70.  
  71. EXPR                        *fold-expr*
  72.  
  73. The folds are automatically defined by their foldlevel, like with the "indent"
  74. method.  The value of the 'foldexpr' option is evaluated to get the foldlevel
  75. of a line.  Examples:
  76. This will create a fold for all consecutive lines that start with a Tab: >
  77.     :set foldexpr=getline(v:lnum)[0]==\"\\t\"
  78. This will call a function to compute the fold level: >
  79.     :set foldexpr=MyFoldLevel(v:lnum)
  80. This will make a fold out of paragraphs separated by blank lines: >
  81.     :set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
  82. this does the same: >
  83.     :set foldexpr=getline(v:lnum-1)=~'^\\s*$'&&getline(v:lnum)=~'\\S'?'>1':1
  84.  
  85. Note that backslashes must be used to escape characters that ":set" handles
  86. differently (space, backslash, double quote, etc., see |option-backslash|).
  87.  
  88. These are the conditions with which the expression is evaluated:
  89. - The current buffer and window are set for the line.
  90. - The variable "v:lnum" is set to the line number.
  91. - The result is used for the fold level in this way:
  92.   value            meaning ~
  93.   0            the line is not in a fold
  94.   1, 2, ..        the line is in a fold with this level
  95.   -1            the fold level is undefined, use the fold level of a
  96.             line before or after this line, whichever is the
  97.             lowest.
  98.   "="            use fold level from the previous line
  99.   "a1", "a2", ..    add one, two, .. to the fold level of the previous
  100.             line
  101.   "s1", "s2", ..    subtract one, two, .. from the fold level of the
  102.             previous line
  103.   "<1", "<2", ..    a fold with this level ends at this line
  104.   ">1", ">2", ..    a fold with this level starts at this line
  105.  
  106. It is not required to mark the start (end) of a fold with ">1" ("<1"), a fold
  107. will also start (end) when the fold level is higher (lower) than the fold
  108. level of the previous line.
  109.  
  110. There must be no side effects from the expression.  The text in the buffer,
  111. cursor position, the search patterns, options etc. must not be changed.
  112.  
  113. If there is some error in the expression, or the resulting value isn't
  114. recognized, there is no error message and the fold level will be zero.
  115. For debugging the 'debug' option can be set to "msg", the error messages will
  116. be visible then.
  117.  
  118. Note: Since the expression has to be evaluated for every line, this fold
  119. method can be very slow!
  120.  
  121. Try to avoid the "=", "a" and "s" return values, since Vim often has to search
  122. backwards for a line for which the fold level is defined.  Using foldlevel()
  123. can help here.  Example: >
  124.     " extra is level compared to previous line
  125.     let lvl = foldlevel(v:lnum - 1)
  126.     if lvl >= 0
  127.       return lvl + extra
  128.     endif
  129.     " return "=", "a" or "s"
  130.  
  131.  
  132. SYNTAX                        *fold-syntax*
  133.  
  134. A fold is defined by syntax items that have the "fold" argument. |:syn-fold|
  135.  
  136. The fold level is defined by nesting folds.  The nesting of folds is limited
  137. with 'foldnestmax'.
  138.  
  139.  
  140. DIFF                        *fold-diff*
  141.  
  142. The folds are automatically defined for text that is not part of a change or
  143. close to a change.
  144.  
  145. This method only works properly when the 'diff' option is set for the current
  146. window and changes are being displayed.  Otherwise the whole buffer will be
  147. one big fold.
  148.  
  149. The 'diffopt' option can be used to specify the context.  That is, the number
  150. of lines between the fold and a change that are not included in the fold.  For
  151. example, to use a context of 8 lines: >
  152.     :set diffopt=filler,context:8
  153. The default context is six lines.
  154.  
  155.  
  156. MARKER                        *fold-marker*
  157.  
  158. Markers in the text tell where folds start and end.  This allows you to
  159. precisely specify the folds.  This will allow deleting and putting a fold,
  160. without the risk of including the wrong lines.  The 'foldtext' option is
  161. normally set such that the text before the marker shows up in the folded line.
  162. This makes it possible to give a name to the fold.
  163.  
  164. Markers can have a level included, or can use matching pairs.  Including a
  165. level is easier, you don't have to add end markers and avoid problems with
  166. non-matching marker pairs.  Example: >
  167.     /* global variables {{{1 */
  168.     int varA, varB;
  169.  
  170.     /* functions {{{1 */
  171.     /* funcA() {{{2 */
  172.     void funcA() {}
  173.  
  174.     /* funcB() {{{2 */
  175.     void funcB() {}
  176.  
  177. A fold starts at a "{{{" marker.  The following number specifies the fold
  178. level.  What happens depends on the difference between the current fold level
  179. and the level given by the marker:
  180. 1. If a marker with the same fold level is encountered, the previous fold
  181.    ends and another fold with the same level starts.
  182. 2. If a marker with a higher fold level is found, a nested fold is started.
  183. 3. if a marker with a lower fold level is found, all folds up to and including
  184.    this level end and a fold with the specified level starts.
  185.  
  186. The number indicates the fold level.  A zero cannot be used.
  187. You can use "}}}" with a digit to indicate the level of the fold that
  188. ends.  The fold level of the following line will be one less than the
  189. indicated level.  Note that Vim doesn't look back to the level of the matching
  190. marker (that would take too much time).  Example: >
  191.  
  192.     {{{1
  193.     fold level here is 1
  194.     {{{3
  195.     fold level here is 3
  196.     }}}3
  197.     fold level here is 2
  198.  
  199. You can also use matching pairs of "{{{" and "}}}" markers to define folds.
  200. Each "{{{" increases the fold level by one, each "}}}" decreases the fold
  201. level by one.  Be careful to keep the markers matching!  Example: >
  202.  
  203.     {{{
  204.     fold level here is 1
  205.     {{{
  206.     fold level here is 2
  207.     }}}
  208.     fold level here is 1
  209.  
  210. You can mix using markers with a number and without a number.  A useful way of
  211. doing this is to use numbered markers for large folds, and unnumbered markers
  212. locally in a function.  For example use level one folds for the sections of
  213. your file like "structure definitions", "local variables" and "functions".
  214. Use level 2 markers for each definition and function,  Use unnumbered markers
  215. inside functions.  When you make changes in a function to split up folds, you
  216. don't have to renumber the markers.
  217.  
  218. The markers can be set with the 'foldmarker' option.  It is recommended to
  219. keep this at the default value of "{{{,}}}", so that files can be exchanged
  220. between Vim users.  Only change it when it is required for the file (e.g., it
  221. contains markers from another folding editor, or the default markers cause
  222. trouble for the language of the file).
  223.  
  224.                             *fold-create-marker*
  225. "zf" can be used to create a fold defined by markers.  Vim will insert the
  226. markers for you.  Vim will append the start and end marker, as specified with
  227. 'foldmarker'.  The markers are appended to the end of the line.
  228. 'commentstring' is used if it isn't empty.
  229. This does not work properly when:
  230. - The line already contains a marker with a level number.  Vim then doesn't
  231.   know what to do.
  232. - Folds nearby use a level number in their marker which gets in the way.
  233. - The line is inside a comment, 'commentstring' isn't empty and nested
  234.   comments don't work.  For example with C: adding /* {{{ */ inside a comment
  235.   will truncate the existing comment.  Either put the marker before or after
  236.   the comment, or add the marker manually.
  237. Generally it's not a good idea to let Vim create markers when you already have
  238. markers with a level number.
  239.  
  240.                             *fold-delete-marker*
  241. "zd" can be used to delete a fold defined by markers.  Vim will delete the
  242. markers for you.  Vim will search for the start and end markers, as specified
  243. with 'foldmarker', at the start and end of the fold.  When the text around the
  244. marker matches with 'commentstring', that text is deleted as well.
  245. This does not work properly when:
  246. - A line contains more than one marker and one of them specifies a level.
  247.   Only the first one is removed, without checking if this will have the
  248.   desired effect of deleting the fold.
  249. - The marker contains a level number and is used to start or end several folds
  250.   at the same time.
  251.  
  252. ==============================================================================
  253. 2. Fold commands                *fold-commands*
  254.  
  255. All folding commands start with "z".  Hint: the "z" looks like a folded piece
  256. of paper, if you look at it from the side.
  257.  
  258.  
  259. CREATING AND DELETING FOLDS ~
  260.                             *zf* *E350*
  261. zf{motion}  or
  262. {Visual}zf    Operator to create a fold.
  263.         This only works when 'foldmethod' is "manual" or "marker".
  264.         The new fold will be closed for the "manual" method.
  265.         'foldenable' will be set.
  266.         Also see |fold-create-marker|.
  267.  
  268.                             *zF*
  269. zF        Create a fold for N lines.  Works like "zf".
  270.  
  271. :{range}fo[ld]                        *:fold* *:fo*
  272.         Create a fold for the lines in {range}.  Works like "zf".
  273.  
  274.                             *zd* *E351*
  275. zd        Delete one fold at the cursor.  When the cursor is on folded
  276.         line, that fold is deleted.  Nested folds are moved one level
  277.         up.  In Visual mode all folds (partially) in the selected area
  278.         are deleted.  Careful: This easily deletes more folds than you
  279.         expect and there is no undo.
  280.         This only works when 'foldmethod' is "manual" or "marker".
  281.         Also see |fold-delete-marker|.
  282.  
  283.                             *zD*
  284. zD        Delete folds recursively at the cursor.  In Visual mode all
  285.         folds (partially) in the selected area and all nested folds in
  286.         them are deleted.
  287.         This only works when 'foldmethod' is "manual" or "marker".
  288.         Also see |fold-delete-marker|.
  289.  
  290.                             *zE* *E352*
  291. zE        Eliminate all folds in the window.
  292.         This only works when 'foldmethod' is "manual" or "marker".
  293.         Also see |fold-delete-marker|.
  294.  
  295.  
  296. OPENING AND CLOSING FOLDS ~
  297.                             *zo*
  298. zo        Open one fold under the cursor.  When a count is given, that
  299.         many folds deep will be opened.  In Visual mode one level of
  300.         folds is opened for all lines in the selected area.
  301.  
  302.                             *zO*
  303. zO        Open all folds under the cursor recursively.  In Visual mode
  304.         it opens all folds that are in the selected area, also those
  305.         that are only partly selected.
  306.  
  307.                             *zc*
  308. zc        Close one fold under the cursor.  When a count is given, that
  309.         many folds deep are closed.  In Visual mode one level of folds
  310.         is closed for all lines in the selected area.
  311.         'foldenable' will be set.
  312.  
  313.                             *zC*
  314. zC        Close all folds under the cursor recursively.  In Visual mode
  315.         it closes all folds that are in the selected area, also those
  316.         that are only partly selected.
  317.         'foldenable' will be set.
  318.  
  319.                             *za*
  320. za        When on a closed fold: open it. When folds are nested, you
  321.         may have to use "za" several times.  When a count is given,
  322.         that many closed folds are opened.
  323.         When on an open fold: close it and set 'foldenable'.  This
  324.         will only close one level, since using "za" again will open
  325.         the fold.  When a count is given that many folds will be
  326.         closed (that's not the same as repeating "za" that many
  327.         times).
  328.  
  329.                             *zA*
  330. zA        When on a closed fold: open it recursively.
  331.         When on an open fold: close it recursively and set
  332.         'foldenable'.
  333.  
  334.                             *zv*
  335. zv        View cursor line: Open just enough folds to make the line in
  336.         which the cursor is located not folded.
  337.  
  338.                             *zx*
  339. zx        Update folds: Undo manually opened and closed folds: re-apply
  340.         'foldlevel', then do "zv": View cursor line.
  341.  
  342.                             *zX*
  343. zX        Undo manually opened and closed folds: re-apply 'foldlevel'.
  344.  
  345.                             *zm*
  346. zm        Fold more: Subtract one from 'foldlevel'.  If 'foldlevel' was
  347.         already zero nothing happens.
  348.         'foldenable' will be set.
  349.  
  350.                             *zM*
  351. zM        Close all folds: set 'foldlevel' to 0.
  352.         'foldenable' will be set.
  353.  
  354.                             *zr*
  355. zr        Reduce folding: Add one to 'foldlevel'.
  356.  
  357.                             *zR*
  358. zR        Open all folds.  This sets 'foldlevel' to highest fold level.
  359.  
  360.                             *:foldo* *:foldopen*
  361. :{range}foldo[pen][!]
  362.         Open folds in {range}.  When [!] is added all folds are
  363.         opened.  Useful to see all the text in {range}.  Without [!]
  364.         one level of folds is opened.
  365.  
  366.                             *:foldc* *:foldclose*
  367. :{range}foldc[lose][!]
  368.         Close folds in {range}.  When [!] is added all folds are
  369.         closed.  Useful to hide all the text in {range}.  Without [!]
  370.         one level of folds is closed.
  371.  
  372.                             *zn*
  373. zn        Fold none: reset 'foldenable'.  All folds will be open.
  374.  
  375.                             *zN*
  376. zN        Fold normal: set 'foldenable'.  All folds will be as they
  377.         were before.
  378.  
  379.                             *zi*
  380. zi        Invert 'foldenable'.
  381.  
  382.  
  383. MOVING OVER FOLDS ~
  384.                             *[z*
  385. [z        Move to the start of the current open fold.  If already at the
  386.         start, move to the start of the fold that contains it.  If
  387.         there is no containing fold, the command fails.
  388.         When a count is used, repeats the command N times.
  389.  
  390.                             *]z*
  391. ]z        Move to the end of the current open fold.  If already at the
  392.         end, move to the end of the fold that contains it.  If there
  393.         is no containing fold, the command fails.
  394.         When a count is used, repeats the command N times.
  395.  
  396.                             *zj*
  397. zj        Move downwards. to the start of the next fold.  A closed fold
  398.         is counted as one fold.
  399.         When a count is used, repeats the command N times.
  400.  
  401.                             *zk*
  402. zk        Move upwards to the end of the previous fold.  A closed fold
  403.         is counted as one fold.
  404.         When a count is used, repeats the command N times.
  405.  
  406.  
  407. EXECUTING COMMANDS ON FOLDS ~
  408.  
  409. :[range]foldd[oopen] {cmd}            *:foldd* *:folddoopen*
  410.         Execute {cmd} on all lines that are not in a closed fold.
  411.         When [range] is given, only these lines are used.
  412.         Each time {cmd} is executed the cursor is positioned on the
  413.         line it is executed for.
  414.         This works like the ":global" command: First all lines that
  415.         are not in a closed fold are marked.  Then the {cmd} is
  416.         executed for all marked lines.  Thus when {cmd} changes the
  417.         folds, this has no influence on where it is executed (except
  418.         when lines are deleted, of course).
  419.         Example: >
  420.             :folddoopen s/end/loop_end/ge
  421. <        Note the use of the "e" flag to avoid getting an error message
  422.         where "end" doesn't match.
  423.  
  424. :[range]folddoc[losed] {cmd}            *:folddoc* *:folddoclosed*
  425.         Execute {cmd} on all lines that are in a closed fold.
  426.         Otherwise like ":folddoopen".
  427.  
  428. ==============================================================================
  429. 3. Fold options                    *fold-options*
  430.  
  431. COLORS                            *fold-colors*
  432.  
  433. The colors of a closed fold are set with the Folded group |hl-Folded|.  The
  434. colors of the fold column are set with the FoldColumn group |hl-FoldColumn|.
  435. Example to set the colors: >
  436.  
  437.     :highlight Folded guibg=grey guifg=blue
  438.     :highlight FoldColumn guibg=darkgrey guifg=white
  439.  
  440.  
  441. FOLDLEVEL                        *fold-foldlevel*
  442.  
  443. 'foldlevel' is a number option: The higher the more folded regions are open.
  444. When 'foldlevel' is 0, all folds are closed.
  445. When 'foldlevel' is positive, some folds closed.
  446. When 'foldlevel' is very high, all folds are open.
  447. 'foldlevel' is applied when it is changed.  After that manually folds can be
  448. opened and closed.
  449. When increased, folds above the new level are opened.  No manually opened
  450. folds will be closed.
  451. When decreased, folds above the new level are closed.  No manually closed
  452. folds will be opened.
  453.  
  454.  
  455. FOLDTEXT                        *fold-foldtext*
  456.  
  457. 'foldtext' is a string option that specifies an expression.  This expression
  458. is evaluated to obtain the text displayed for a closed fold.  Example: >
  459.  
  460.     :set foldtext=v:folddashes.substitute(getline(v:foldstart),'/\\*\\\|\\*/\\\|{{{\\d\\=','','g')
  461.  
  462. This shows the first line of the fold, with "/*", "*/" and "{{{" removed.
  463. Note the use of backslashes to avoid some characters to be interpreted by the
  464. ":set" command.  It's simpler to define a function and call that: >
  465.  
  466.     :set foldtext=MyFoldText()
  467.     :function MyFoldText()
  468.     :  let line = getline(v:foldstart)
  469.     :  let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g')
  470.     :  return v:folddashes . sub
  471.     :endfunction
  472.  
  473. Evaluating 'foldtext' is done in the |sandbox|.  The current window is set to
  474. the window that displays the line.  Errors are ignored.
  475.  
  476. The default value is |foldtext()|.  This returns a reasonable text for most
  477. types of folding.  If you don't like it, you can specify your own 'foldtext'
  478. expression.  It can use these special Vim variables:
  479.     v:foldstart    line number of first line in the fold
  480.     v:foldend    line number of last line in the fold
  481.     v:folddashes    a string that contains dashes to represent the
  482.             foldlevel.
  483.     v:foldlevel    the foldlevel of the fold
  484.  
  485. In the result a TAB is replaced with a space and unprintable characters are
  486. made into printable characters.
  487.  
  488. The resulting line is truncated to fit in the window, it never wraps.
  489. When there is room after the text, it is filled with the character specified
  490. by 'fillchars'.
  491.  
  492. Note that backslashes need to be used for characters that the ":set" command
  493. handles differently: Space, backslash and double-quote. |option-backslash|
  494.  
  495.  
  496. FOLDCOLUMN                        *fold-foldcolumn*
  497.  
  498. 'foldcolumn' is a number, which sets the width for a column on the side of the
  499. window to indicate folds.  When it is zero, there is no foldcolumn.  A normal
  500. value is 4 or 5.  The minimal useful value is 2.  The maximum is 12.
  501.  
  502. An open fold is indicated with a column that has a '-' at the top and '|'
  503. characters below it.  This column stops where the open fold stops.  When folds
  504. nest, the nested fold is one character right of the fold it's contained in.
  505.  
  506. A closed fold is indicated with a '+'.
  507.  
  508. Where the fold column is too narrow to display all nested folds, digits are
  509. shown to indicate the nesting level.
  510.  
  511. The mouse can also be used to open and close folds by clicking in the
  512. fold column:
  513. - Click on a '+' to open the closed fold at this row.
  514. - Click on any other non-blank character to close the open fold at this row
  515.  
  516.  
  517. OTHER OPTIONS
  518.  
  519. 'foldenable'  'fen':    Open all folds while set.
  520. 'foldexpr'    'fde':    Expression used for "expr" folding.
  521. 'foldignore'  'fdi':    Characters used for "indent" folding.
  522. 'foldmarker'  'fmr':    Defined markers used for "marker" folding.
  523. 'foldmethod'  'fdm':    Name of the current folding method.
  524. 'foldminlines' 'fml':    Minimum number of screen lines for a fold to be
  525.             displayed closed.
  526. 'foldnestmax' 'fdn':    Maximum nesting for "indent" and "syntax" folding.
  527. 'foldopen'    'fdo':    Which kinds of commands open closed folds.
  528. 'foldclose'   'fcl':    When the folds not under the cursor are closed.
  529.  
  530. ==============================================================================
  531. 4. Behavior of folds                    *fold-behavior*
  532.  
  533. When moving the cursor upwards or downwards and when scrolling, the cursor
  534. will move to the first line of a sequence of folded lines.  When the cursor is
  535. already on a folded line, it moves to the next unfolded line or the next
  536. closed fold.
  537.  
  538. While the cursor is on folded lines, the cursor is always displayed in the
  539. first column.  The ruler does show the actual cursor position, but since the
  540. line is folded, it cannot be displayed there.
  541.  
  542. Movement commands handle a sequence of folded lines like an empty line.  For
  543. example, the "w" command stops once in the first column.
  544.  
  545. When in Insert mode, the cursor line is never folded.  That allows you to see
  546. what you type!
  547.  
  548. When using an operator, a closed fold is included as a whole.  Thus "dl"
  549. deletes the whole closed fold under the cursor.
  550.  
  551. For Ex commands the range is adjusted to always start at the first line of a
  552. fold and end at the last line of a fold.  Thus this command: >
  553.     :s/foo/bar/g
  554. when used with the cursor on a closed fold, will replace "foo" with "bar" in
  555. all lines of the fold.
  556. This does not happen for |:folddoopen| and |:folddoclosed|.
  557.  
  558. When editing a buffer that has been edited before, the last used folding
  559. settings are used again.  For manual folding the defined folds are restored.
  560. For all folding methods the manually opened and closed folds are restored.
  561. If this buffer has been edited in this window, the values from back then are
  562. used.  Otherwise the values from the window where the buffer was edited last
  563. are used.
  564.  
  565. ==============================================================================
  566.  vim:tw=78:ts=8:ft=help:norl:
  567.